clear all
set more off
set maxvar 10000


    file open unstack using "$Mydirectory2/appendix_e/unstack_stata2tex.tex", write replace
        foreach s in anes avtmh57 avtmh76 gss nfs nlsmw nlsom nlsy79 nlsym66 nlsyw68 nsba nsfh ocg62 ocg73 psid  {

    *--------------------------------------*
    *  STEP 1: DEFINE LOCALS   
    *--------------------------------------*
        if "`s'"=="anes" {
            local name "American National Election Survey, 1956--1970"
        }            
        if "`s'"=="avtmh57" {
            local name "Americans View Their Mental Health, 1957"      
             }
        if "`s'"=="avtmh76" {
            local name "Americans View Their Mental Health, 1976"         
              }
        if "`s'"=="gss" {
            local name "General Social Surveys, 1977--2018"          
              }
        if "`s'"=="nfs" {
            local name "National Fertility Study, 1970"        
             }
        if "`s'"=="nlsmw" {
            local name "National Longitudinal Survey of Mature Women, 1967"          
              }
        if "`s'"=="nlsom" {
            local name "National Longitudinal Survey of Older Men, 1966"          
              }
        if "`s'"=="nlsy79" {
            local name "National Longitudinal Survey of Youth, 2002"           
             }
        if "`s'"=="nlsym66" {
            local name "National Longitudinal Survey of Young Men, 1981"         
              }
        if "`s'"=="nlsyw68" {
            local name "National Longitudinal Survey of Young Women, 1988"         
               }
        if "`s'"=="nsba" {
            local name "National Survey of Black Americans, 1979--1980"          
              }
        if "`s'"=="nsfh" {
            local name "National Survey of Families \& Households, 1987--1988"          
              }
        if "`s'"=="ocg62" {
            local name "Occupational Changes in a Generation, 1962"           
             }
        if "`s'"=="ocg73" {
            local name "Occupational Changes in a Generation, 1973"          
              }
        if "`s'"=="psid" {
            local name "Panel Study of Income Dynamics, 1997 \& 2017"             
        }

		use "$Mydirectory1/3_Output/2_PooledData_analysis.dta", clear 
         keep if baseline_sample==1

         file write unstack "`name'"    

            foreach d of numlist 1/7 {
                count if data=="`s'" & decade==19`d'0
                    local bsample = `r(N)'
                    local bsample2_`d': display %-09.0fc `bsample'

                if `r(N)'==0 file write unstack "& ---"
                else file write unstack "& `bsample2_`d''"

                if `d'==7 file write unstack "\\" _n
            }
        }

    file close unstack
